﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using verk2013_project_group12.Models;

namespace verk2013_project_group12.Code
{
    // Class for distributing player scores after the completion of game instances
    /* It works like this:
     * Total score of all players is calculated
     * Total price is calculated by the formula - total score * 200 / number of players 
     * For every player, his ratio of the total score * total price is deducted from their account,
       so players with high scores commit more than players with low scores
     * The winner is awarded the total price
     */

    public class Scoring
    {
        private UserProfile winner = new UserProfile();
        private List<UserProfile> players = new List<UserProfile>();
        private int totalPoints;
        private double totalPrice;
        public Scoring(UserProfile theWinner, List<UserProfile> thePlayers)
        {
            winner = theWinner;
            players = thePlayers;
            totalPoints = (from p in thePlayers
                           select p.OverallScore).Sum();
            totalPrice = totalPoints / 200 * thePlayers.Count();
        }
        public int GetTotalPoints()
        {
            return totalPoints;
        }

        public double GetTotalPrice()
        {
            return totalPrice;
        }

        public double GetPriceQuota(UserProfile thePlayer)
        {
            return ((double)thePlayer.OverallScore / totalPoints) * totalPrice;
        }

        public void GiveScore()
        {
            foreach (var player in players)
            {
                player.OverallScore = player.OverallScore - Convert.ToInt32(GetPriceQuota(player));
            }
            winner.OverallScore = winner.OverallScore + Convert.ToInt32(totalPrice);
        }
    }
}